package com.starlight.easybuyprictice.service;


import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author yfy2001
 * @date 2022/11/28 21:23
 */
@Service
public class RedisService {
    private final StringRedisTemplate stringRedisTemplate ;

    public RedisService(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }


    public boolean hasKey(String key){
        return stringRedisTemplate.hasKey(key);
    }
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }
    public void set(String key,String value){
        stringRedisTemplate .opsForValue().set(key,value);
    }
    public void set(String key,String value,long timeout) {
        stringRedisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
    }
    public void set(String key,String value,long timeout,TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key,value,timeout,unit);
    }
    public long getExpire(String key,TimeUnit unit) {
        return stringRedisTemplate.getExpire(key,unit);
    }
    public long getExpire(String key) {
        return getExpire(key,TimeUnit.SECONDS);
    }

    public void delete(String key){
        stringRedisTemplate.delete(key);
    }

}
